package com.sxj.conmon.util.forkjoin;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Task extends RecursiveTask<List<String>> {
	
	private static final long serialVersionUID = 7014789255446064272L;
	
	private List<Student> products;
	private int size;
	private ReentrantReadWriteLock lock;
	private StringBuffer aa;
	
	public Task(List<Student> list, int size, ReentrantReadWriteLock lock,StringBuffer aa) {
		super();
		this.products = list;
		this.size = size;
		this.lock = lock;
		this.aa = aa;
	}

	@Override
	protected List<String> compute() {
		List<String> aaa = new ArrayList<String>();
		List<String> str = new ArrayList<String>();
		if (size <= 1) {
			aa.append("woshis;");
			for (Student ss: products) {
				str.add(ss.getName());
			}
        } else {  
            int middle = size / 2;
            List<Student> list1 = products.subList(0, middle);
            Task task1 = new Task(list1,list1.size(),lock,aa);
            
            List<Student> list2 = products.subList(middle, products.size());
            Task task2 = new Task(list2,list2.size(),lock,aa);
            task1.fork();
            task2.fork();
            List<String> left = task1.join();
            List<String> right = task2.join();
            str.addAll(aaa);
            str.addAll(left);
            str.addAll(right);
        }
		return str;
	}

}
